package com.zxl.yjb.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyRealInfoActivity extends BaseActivity {

    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_userName)
    TextView tvUserName;
    @BindView(R.id.tv_idCardNum)
    TextView tvIdCardNum;
    @BindView(R.id.iv_idCard)
    ImageView ivIdCard;

    @BindView(R.id.tv_msg)
    TextView tv_msg;
    @BindView(R.id.tv_verification)
    TextView tv_verification;


    private LoginSucessBean loginSucessBean;

    @Override
    protected void initView() {
        tvPageTitle.setText("实名认证");
        Object bean = SaveBeanUtil.getBean(this, Const.VIPINFO);
        if (bean != null) {
            loginSucessBean = (LoginSucessBean) bean;
        }
        refreshUserInfo();
    }

    /**
     * 获取个人信息
     */
    private void refreshUserInfo() {
        DataManager dataManager = new DataManager();
        dataManager.getUserInfo(new ProgressObserver<LoginSucessBean>(new ObserverOnNextListener<LoginSucessBean>() {
            @Override
            public void onNext(LoginSucessBean loginSucessBean) {
                if (loginSucessBean == null)
                    return;
                MyRealInfoActivity.this.loginSucessBean = loginSucessBean;
                SaveBeanUtil.putBean(getApplicationContext(), Const.VIPINFO, loginSucessBean);
                setUserInfo(loginSucessBean);
            }
        }, this), loginSucessBean.getUid() + "");
    }

    private void setUserInfo(LoginSucessBean loginSucessBean) {
        tvUserName.setText(loginSucessBean.getIdcard_name());
        tvIdCardNum.setText(loginSucessBean.getIdcard_num());
        if (loginSucessBean.getIdcard_state().equals("1")) {
            tvStatus.setText("审核中");
        } else if (loginSucessBean.getIdcard_state().equals("2")) {
            tvStatus.setText("未通过");
            tv_verification.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(loginSucessBean.getIdcard_nopass_msg())) {
                tv_msg.setVisibility(View.VISIBLE);
                tv_msg.setText(loginSucessBean.getIdcard_nopass_msg());
            }

        } else if (loginSucessBean.getIdcard_state().equals("3")) {
            tvStatus.setText("已认证");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_real_info);
        ButterKnife.bind(this);
        initView();
    }


    @OnClick({R.id.iv_back, R.id.tv_verification})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_verification:
                startActivity(new Intent(MyRealInfoActivity.this,RealNameAuthenticationActivity.class));
                finish();
                break;
        }
    }
}
